import {claimUsedPartDealCategory} from '../../Enum';

export const onSelectPart = (records, claimWorkItemDetails, claimMaterialDetails) => {
    records.forEach(r => {
        const indexMaterial = claimMaterialDetails.findIndex(v => v.usedPartId === r.id);
        if(indexMaterial === -1)
            claimMaterialDetails.push({
                isCaused: false,
                usedPartId: r.id,
                usedPartCode: r.code,
                usedPartName: r.name,
                newPartId: r.id,
                newPartCode: r.code,
                newPartName: r.name,
                unit: r.unit,
                price: r.price * r.priceRate,
                quantity: 1,
                materialFee: r.price * r.priceRate,
                usedPartDealCategory: claimUsedPartDealCategory.销毁,
                mgmtRate: r.mgmtFeeRate,
                mgmtFee: r.mgmtFeeRate ? r.price * r.mgmtFeeRate * r.priceRate : 0,
                priceRate: r.priceRate,
                totalFee: (r.price * r.priceRate) + (r.mgmtFeeRate ? r.price * r.mgmtFeeRate * r.priceRate : 0)
            });
        r.workItems.forEach(v => {
            const index = claimWorkItemDetails.findIndex(o => o.workItemId === v.id);
            if(index === -1)
                claimWorkItemDetails.push({
                    usedPartId: r.id,
                    usedPartCode: r.code,
                    workItemId: v.id,
                    workItemCode: v.code,
                    workItemName: v.name,
                    laborHour: v.laborHour,
                    laborPrice: v.laborPrice,
                    laborFee: v.laborHour / 100 * v.laborPrice,
                    remark: v.remark
                });
        });
    });
    return {
        claimWorkItemDetails: [...claimWorkItemDetails],
        claimMaterialDetails: [...claimMaterialDetails]
    };
};
export const onSelectPartAccessory = (records, details) => {
    records.forEach(r => {
        const index = details.findIndex(v => v.usedPartId === r.id);
        if(index === -1)
            details.push({
                isCaused: false,
                usedPartId: r.id,
                usedPartCode: r.code,
                usedPartName: r.name,
                unit: r.unit,
                price: r.price * r.priceRate,
                usedPartDealCategory: claimUsedPartDealCategory.销毁,
                materialFee: r.price * r.priceRate,
                priceRate: r.priceRate,
                accessorySource: r.partAssistPartSource,
                partId: r.partId,
                minPackSize: r.minPackSize,
                maxQty: r.maxQty,
                usedQty: 0,
                quantity: 0
            });
    });
    return {
        details: [...details]
    };
};
export const onChangeSwitchSubDealer = (dealer, data) => ({
    data: Object.assign({}, data, {
        subDealerId: dealer.id,
        subDealerCode: dealer.code,
        subDealerName: dealer.name,
        currency: dealer.currency,
        country: dealer.countryName,
        subClaimId: '',
        subClaimCode: ''
    })
});
export const onChangeSwitchVehicle = (vehicle, data) => ({
    vehicle: Object.assign({}, data, {
        vehicleSoldId: vehicle.id,
        vin: vehicle.vin,
        mileage: undefined,
        engineCode: vehicle.engineCode,
        saleDate: vehicle.saleDate,
        shippingDate: vehicle.shippingDate,
        customerName: vehicle.customerName,
        cellPhoneNumber: vehicle.cellPhoneNumber,
        vehicleStatus: vehicle.status,
        repairType: undefined,
        claimApplyId: '',
        claimApplyCode: '',
        claimApplyCreateTime: undefined,
        transmissionSn: vehicle.transmissionSn,
        productCategoryCode: vehicle.productCategoryCode,
        productCategoryName: vehicle.productCategoryName,
        annoucementNumber: vehicle.annoucementNumber,
        operationsNature: vehicle.operationsNature,
        isKD: vehicle.isKD,
        currency: vehicle.currency,
        wholesaleDealerCode: vehicle.wholesaleDealerCode,
        wholesaleDealerName: vehicle.wholesaleDealerName,
        country: vehicle.country,
        shipDate: vehicle.shipDate,
        companyName: vehicle.companyName,
        address: vehicle.address,
        email: vehicle.email,
        wholesaleDealerId: vehicle.wholesaleDealerId,
        salesDealerCode: data.salesDealerCode,
        salesDealerName: data.salesDealerName,
        isNeedPictureDealer: data.isNeedPicture,
        productionDate: vehicle.productionDate
    })
});
export const onChangeClaimApplyDetail = data => ({
    vehicleSoldId: data.vehicleSoldId,
    vin: data.vin,
    mileage: data.mileage,
    engineCode: data.engineCode,
    saleDate: data.saleDate,
    shippingDate: data.shippingDate,
    customerName: data.customerName,
    cellPhoneNumber: data.cellPhoneNumber,
    vehicleStatus: data.vehicleStatus,
    repairType: data.repairType,
    claimApplyId: data.id,
    claimApplyCode: data.code,
    claimApplyCreateTime: data.createTime,
    subClaimId: '',
    subClaimCode: '',
    transmissionSn: data.transmissionSn,
    productCategoryCode: data.productCategoryCode,
    productCategoryName: data.productCategoryName,
    annoucementNumber: data.annoucementNumber,
    operationsNature: data.operationsNature,
    isKD: data.isKD,
    currency: data.currency,
    wholesaleDealerId: data.wholesaleDealerId,
    wholesaleDealerCode: data.wholesaleDealerCode,
    wholesaleDealerName: data.wholesaleDealerName,
    country: data.country,
    shipDate: data.shipDate,
    companyName: data.companyName,
    address: data.address,
    email: data.email,
    salesDealerCode: data.salesDealerCode,
    salesDealerName: data.salesDealerName,
    brandId: data.brandId,
    brandCode: data.brandCode,
    brandName: data.brandName,
    faultModeId: data.faultModeId,
    faultModeCode: data.faultModeCode,
    faultModeName: data.faultModeName,
    faultDescription: data.faultDescription,
    supplierId: data.supplierId,
    supplierCode: data.supplierCode,
    supplierName: data.supplierName,
    partPurchasedDate: data.partPurchasedDate,
    subClaimApplyCreateTime: data.createTime,
    isOtherArea: data.isOtherArea,
    faultType: data.faultType,
    faultItemId: data.faultItemId,
    faultItemCode: data.faultItemCode,
    faultItemName: data.faultItemName,
    checkResul: data.checkResult,
    artisan: data.artisan,
    artisanResult: data.artisanResult,
    checkDate: data.checkDate,
    checkApproveComment: data.dealerApproveComment,
    repairCode: data.repairCode,
    repairDate: data.repairDate,
    isNeedPictureDealer: data.isNeedPictureDealer,
    isNeedPictureSupplier: data.isNeedPictureSupplier,
    productionDate: data.productionDate
});
export const onChangeSubClaimDetail = data => ({
    subClaimId: data.id,
    subClaimCode: data.code,
    vehicleSoldId: data.vehicleSoldId,
    vin: data.vin,
    mileage: data.mileage,
    engineCode: data.engineCode,
    saleDate: data.saleDate,
    shippingDate: data.shippingDate,
    customerName: data.customerName,
    cellPhoneNumber: data.cellPhoneNumber,
    vehicleStatus: data.vehicleStatus,
    repairType: data.repairType,
    transmissionSn: data.transmissionSn,
    productCategoryCode: data.productCategoryCode,
    productCategoryName: data.productCategoryName,
    annoucementNumber: data.annoucementNumber,
    operationsNature: data.operationsNature,
    isKD: data.isKD,
    currency: data.currency,
    wholesaleDealerId: data.wholesaleDealerId,
    wholesaleDealerCode: data.wholesaleDealerCode,
    wholesaleDealerName: data.wholesaleDealerName,
    country: data.country,
    shipDate: data.shipDate,
    companyName: data.companyName,
    address: data.address,
    email: data.email,
    subDealerId: data.salesDealerId,
    salesDealerCode: data.salesDealerCode,
    salesDealerName: data.salesDealerName,
    brandId: data.brandId,
    brandCode: data.brandCode,
    brandName: data.brandName,
    faultModeId: data.faultModeId,
    faultModeCode: data.faultModeCode,
    faultModeName: data.faultModeName,
    faultDescription: data.faultDescription,
    supplierId: data.supplierId,
    supplierCode: data.supplierCode,
    supplierName: data.supplierName,
    partPurchasedDate: data.partPurchasedDate,
    subClaimApplyCreateTime: data.createTime,
    isOtherArea: data.isOtherArea,
    faultType: data.faultType,
    faultItemId: data.faultItemId,
    faultItemCode: data.faultItemCode,
    faultItemName: data.faultItemName,
    checkResul: data.checkResult,
    artisan: data.artisan,
    artisanResult: data.artisanResult,
    checkDate: data.checkDate,
    checkApproveComment: data.dealerApproveComment,
    repairCode: data.repairCode,
    repairDate: data.repairDate,
    isNeedPictureDealer: data.isNeedPictureDealer,
    isNeedPictureSupplier: data.isNeedPictureSupplier,
    productionDate: data.productionDate
});

export const onChangeSwitchVehicleIsOtherArea = (isOtherArea, data) => ({
    vehicle: Object.assign({}, data, {
        vehicleSoldId: '',
        vin: '',
        mileage: undefined,
        engineCode: '',
        saleDate: '',
        shippingDate: '',
        customerName: '',
        cellPhoneNumber: '',
        vehicleStatus: '',
        repairType: undefined,
        claimApplyId: '',
        claimApplyCode: '',
        claimApplyCreateTime: undefined,
        transmissionSn: '',
        productCategoryCode: '',
        productCategoryName: '',
        annoucementNumber: '',
        operationsNature: '',
        isKD: '',
        currency: '',
        wholesaleDealerCode: '',
        wholesaleDealerName: '',
        country: '',
        shipDate: '',
        companyName: '',
        address: '',
        email: '',
        wholesaleDealerId: '',
        productionDate: '',
        isOtherArea
    })
});
